<?php
class CConsultStream
{
	var $LAST_ERROR="";
	var $METHOD = "";

	function GetByID ($ID)
	{
		global $DB;
		$ID = intval($ID);
		if ($ID <= 0)
			return false;

		$strSql = 	"SELECT CS.ID, CS.RESPONSIBLE_ID, CS.OWNER_ID, 
					".$DB->DateToCharFunction("CS.DATE_CREATE", "FULL")." as DATE_CREATE 
					FROM b_consult_stream CS ".
					"WHERE CS.ID='".$ID."'";

		$res = $DB->Query($strSql);
		if($arStream = $res->Fetch())
			return $arStream;

		return false;
	}

	function GetList($arSort = array("ID"=>"ASC"), $arFilter = array(), $CHECK_RIGHTS ='Y')
	{
		global $DB, $USER;
		$arFilter = (is_array($arFilter) ? $arFilter : array());

		static $arFields = array(
			"ID" => 			Array("FIELD_NAME" => "CS.ID", 				"FIELD_TYPE" => "int"),
			"RESPONSIBLE_ID" => Array("FIELD_NAME" => "CS.RESPONSIBLE_ID", 	"FIELD_TYPE" => "int"),
			"OWNER_ID" => 		Array("FIELD_NAME" => "CS.OWNER_ID", 		"FIELD_TYPE" => "int"),
			"DATE_CREATE" => 	Array("FIELD_NAME" => "CS.DATE_CREATE", 	"FIELD_TYPE" => "datetime"),
		);
		$obWhere = new CSQLWhere;
 		$obWhere->SetFields($arFields);

		$uid = CConsultUser::GetCurConsultUser();

		if ($CHECK_RIGHTS=='Y') 
		{
			$bAdmin = (CConsultUser::IsAdmin()) ? 'Y' : 'N'; 
			$bSupportTeam = (CConsultUser::IsConsultTeam()) ? 'Y' : 'N';
			$bSupportClient = (CConsultUser::IsConsultClient()) ? 'Y' : 'N';
		}
		else 
		{
			$bAdmin = 'Y';
			$bSupportTeam = 'Y';
			$bSupportClient = 'Y';
		}

		if ($bAdmin!='Y' && $bSupportTeam!='Y' && $bSupportClient!='Y') 
			return false;

		$extRoleSearch = "";
		if ($bAdmin == 'Y')
		{
			$extRoleSearch = "";
		}
		elseif ($bSupportTeam == 'Y')
		{
			$arFilter[] = array("RESPONSIBLE_ID"=>$uid);
		}
		elseif ($bSupportClient  ==  "Y" && $bSupportTeam != 'Y')
		{
			$arFilter[] = array("OWNER_ID"=>$uid);
		}
		
		$arFilterNew = Array();

		foreach($arFilter as $k=>$value)
			if 	( 	(is_array($value) && count($value))
					||
					(strlen($value)>0 || $value === false)
				)
				$arFilterNew[$k]=$value;

		$strWhere = $obWhere->GetQuery($arFilterNew);
	
		list ($by, $order) = $arSort;

		$by = strtolower($by);
		$order = strtolower($order);

		if($by=="id")
			$strSqlOrder = " ORDER BY CS.ID ";
		elseif($by == "responsible_id")
			$strSqlOrder = " ORDER BY CS.ENTITY_TYPE ";
		elseif($by == "entity_id")
			$strSqlOrder = " ORDER BY CS.ENTITY_ID ";
		elseif($by == "date_create")
			$strSqlOrder = " ORDER BY CS.DATE_CREATE ";
		else
			$strSqlOrder = " ORDER BY CS.ID ";

		if($order=="desc")
			$strSqlOrder .= " desc ";

		$strSql = "
			SELECT
				CS.ID, CS.RESPONSIBLE_ID, CS.OWNER_ID, 
				".$DB->DateToCharFunction("CS.DATE_CREATE", "FULL")." as DATE_CREATE 
			FROM
				b_consult_stream CS".
			(strlen($strWhere)<=0 ? "" : " WHERE ".$strWhere)." ".
			$strSqlOrder;

		$res = $DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
		//var_dump($strSql);
		return $res;
	}

	function Delete ($ID)
	{
		global $DB, $USER;
		$ID = intval($ID);
		if ($ID <= 0)
			return false;

		$delmess = true;
		//Удаляем сообщения
		if ($delmess)
			$strSql = 	"DELETE FROM b_consult_stream ".
						"WHERE ID='".$ID."'";
		$res = $DB->Query($strSql);
		if ($res)
			return true;

		return false;
	}

	function Add($arFields)
	{
		global $DB, $USER;

		$this->LAST_ERROR = "";
		$this->METHOD = "CConsultStream::Add(".__LINE__.")";

		if (!CConsultStream::CheckFields($arFields))
			return false;

		$dbInsert = array(
			"RESPONSIBLE_ID" 	=> $arFields["RESPONSIBLE_ID"],
			"~DATE_CREATE" 		=> $DB->GetNowFunction(),
			"OWNER_ID" 		=> $arFields["OWNER_ID"],
		);

		$strSql = 	"SELECT C.* ".
					"FROM b_consult_stream C ".
					"WHERE C.OWNER_ID='".$dbInsert["OWNER_ID"]."'";

		$res = $DB->Query($strSql);
		if($res->Fetch())
		{
			$strSqlDel = "DELETE FROM b_consult_stream ".
				     "WHERE OWNER_ID='".$dbInsert["OWNER_ID"]."'";
			$DB->Query($strSqlDel);
		}

		$arInsert = $DB->PrepareInsert("b_consult_stream", $dbInsert);
		$strSqlInsert =	"INSERT INTO b_consult_stream(".$arInsert[0].") ".
				"VALUES(".$arInsert[1].")";

		if ($DB->Query($strSqlInsert))
			return $DB->LastID();

		return false;
	}
	
	function CheckFields($arFields, $ID = false)
	{
		if (!is_array($arFields))
			return false;

		if ($ID === false)
		{
			if (!isset($arFields["RESPONSIBLE_ID"]))
				$this->LAST_ERROR .= "RESPONSIBLE_ID not set"."<br>";
		}

		//+CHECK ROLES
		if (isset($arFields["RESPONSIBLE_ID"]))
		{
			if (intval($arFields["RESPONSIBLE_ID"]) > 0)
			{
				$arResp = CUser::GetByID($arFields["RESPONSIBLE_ID"]);
				if (!$arResp->Fetch())
					$this->LAST_ERROR .= "RESPONSIBLE_ID contain not user_id"."<br>";
			}
			else
			{
				$this->LAST_ERROR .= "RESPONSIBLE_ID contain incorrect value"."<br>";
			}
		}

		if (isset ($arFields["DATE_CREATE"]) && !CheckDateTime($arFields["DATE_CREATE"]))
			$this->LAST_ERROR .= "DATE_CREATE contain incorrect value"."<br>";

		if(strlen($this->LAST_ERROR)>0)
			return false;

		return true;

	}
}
?>